package Services

import(
	"github.com/gin-gonic/gin"
	"fmt"
	"strings"
	"net/http"
	"gin/Databases"
	"gin/Common"
)

func DisplaySuggestV2(c *gin.Context) {
	var entity_indexs []Common.EntityIndex
	sql := "select Findex_code1,Findex_name1,Findex_type1,Findex_code2,Findex_name2,Findex_type2,Findex_code3,Findex_name3,Findex_type3,Findex_code4,Findex_name4,Findex_type4,Findex_code5,Findex_name5,Findex_type5,Findex_code6,Findex_name6,Findex_type6,Findex_code7,Findex_name7,Findex_type7,Findex_code8,Findex_name8,Findex_type8 from entity_index where Findex_word=? limit 1;"
	err := Databases.Db.Select(&entity_indexs, sql, c.Query("word"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "query fail",
	        "data": []string{"查询出错，稍后重试"},
	    })
		fmt.Println("exec failed, ", err)
		return
	}

	if len(entity_indexs) != 1 {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "no data",
	        "data": []string{"未找到相关数据"},
	    })
		fmt.Println("no data")
		return	
	}

	var entity_res []string
	if entity_indexs[0].Index_code1 != "" {
		var build strings.Builder
		build.WriteString(Common.GetEntityName(entity_indexs[0].Index_type1))
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_code1)
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_name1)
		entity_res = append(entity_res, build.String())
	}
	if entity_indexs[0].Index_code2 != "" {
		var build strings.Builder
		build.WriteString(Common.GetEntityName(entity_indexs[0].Index_type2))
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_code2)
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_name2)
		entity_res = append(entity_res, build.String())
	}
	if entity_indexs[0].Index_code3 != "" {
		var build strings.Builder
		build.WriteString(Common.GetEntityName(entity_indexs[0].Index_type3))
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_code3)
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_name3)
		entity_res = append(entity_res, build.String())
	}
	if entity_indexs[0].Index_code4 != "" {
		var build strings.Builder
		build.WriteString(Common.GetEntityName(entity_indexs[0].Index_type4))
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_code4)
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_name4)
		entity_res = append(entity_res, build.String())
	}
	if entity_indexs[0].Index_code5 != "" {
		var build strings.Builder
		build.WriteString(Common.GetEntityName(entity_indexs[0].Index_type5))
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_code5)
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_name5)
		entity_res = append(entity_res, build.String())
	}
	if entity_indexs[0].Index_code6 != "" {
		var build strings.Builder
		build.WriteString(Common.GetEntityName(entity_indexs[0].Index_type6))
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_code6)
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_name6)
		entity_res = append(entity_res, build.String())
	}
	if entity_indexs[0].Index_code7 != "" {
		var build strings.Builder
		build.WriteString(Common.GetEntityName(entity_indexs[0].Index_type7))
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_code7)
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_name7)
		entity_res = append(entity_res, build.String())
	}
	if entity_indexs[0].Index_code8 != "" {
		var build strings.Builder
		build.WriteString(Common.GetEntityName(entity_indexs[0].Index_type8))
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_code8)
		build.WriteString("|")
		build.WriteString(entity_indexs[0].Index_name8)
		entity_res = append(entity_res, build.String())
	}
	



	fmt.Println("select succ:", entity_res)

    //c.JSON(http.StatusOK, entity_res)
    c.JSON(http.StatusOK, gin.H{
        "result": 0,
        "msg": "success",
        "data": entity_res,
    })
}


func DisplaySuggest(c *gin.Context) {
	var entity_indexs []Common.EntityIndexV2
	sql := "select Findex_code,Findex_name,Findex_type from entity_index where Findex_word=? limit 8;"
	err := Databases.Db.Select(&entity_indexs, sql, c.Query("word"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "query fail",
	        "data": []string{"查询出错，稍后重试"},
	    })
		fmt.Println("exec failed, ", err)
		return
	}

	var entity_res []string
	for i := 0; i < len(entity_indexs); i++ {
		var build strings.Builder
		build.WriteString(Common.GetEntityName(entity_indexs[i].Index_type))
		build.WriteString("|")
		build.WriteString(entity_indexs[i].Index_code)
		build.WriteString("|")
		build.WriteString(entity_indexs[i].Index_name)
		entity_res = append(entity_res, build.String())
	}

	fmt.Println("select succ:", entity_res)

    //c.JSON(http.StatusOK, entity_res)
    c.JSON(http.StatusOK, gin.H{
        "result": 0,
        "msg": "success",
        "data": entity_res,
    })
}